package com.fangcang.mongodb.impl;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.stereotype.Service;

import com.fangcang.mongodb.MongoDbService;
import com.fangcang.mongodb.model.Customer;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBObject;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;

@Service("mongoDbService")
public class MongodbServiceImpl implements MongoDbService<Customer>{
	
	@Autowired
	private MongoTemplate mongoTemplate;
	
	@SuppressWarnings("unused")
	@Autowired
	private GridFsTemplate gridFsTemplate;
	
	
	public void insert(Customer customer) {
		mongoTemplate.insert(customer);
	}
	
	public void save(Customer obj) {
		mongoTemplate.save(obj);
	}
	
	public List<Customer> findAll(){
		return mongoTemplate.findAll(Customer.class);
	}
	
	public List<Customer> findAllBySort(){
		Query query = new Query();
		query.with(new Sort(Direction.ASC, "firstname").and(new Sort(Direction.ASC, "secondname")));
		return mongoTemplate.findAll(Customer.class);
	}
	
	public Customer findById(String id){
		return mongoTemplate.findById(id, Customer.class);
	}
	
	public List<Customer> findByFirstName(String firstName){
		Query query  = new Query(Criteria.where("firstname").is(firstName));
		return mongoTemplate.find(query, Customer.class);
	}
	
	
	public DeleteResult delete(Customer obj) {
		return mongoTemplate.remove(obj);
	}
	
	public void update(Customer obj) {
		mongoTemplate.update(Customer.class);
	}
	
	/**
     * 存储文件 
     * @param collectionName 集合名 
     * @param file 文件 
     * @param fileid 文件id 
     * @param companyid 文件的公司id 
     * @param filename 文件名称
     */
    public void SaveFile(String collectionName, File file, String fileName, String hotelId, String imageType) {
        try {
            DB db = mongoTemplate.getMongoDbFactory().getLegacyDb();
            // 存储fs的根节点
            GridFS gridFS = new GridFS(db, collectionName);
            GridFSInputFile gfs = gridFS.createFile(file);
            gfs.put("hotelId", hotelId);
            gfs.put("imageType", imageType);
            gfs.put("fileName", fileName);
            gfs.put("contentType", fileName.substring(fileName.lastIndexOf(".")+1));
            gfs.save();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("存储文件时发生错误！！！");
        }
    }

    // 取出文件
    public GridFSDBFile retrieveFileOne(String collectionName, String filename) {
        try {
            DB db = mongoTemplate.getMongoDbFactory().getLegacyDb();
            // 获取fs的根节点
            GridFS gridFS = new GridFS(db, collectionName);
            GridFSDBFile dbfile = gridFS.findOne(filename);
            if (dbfile != null) {
                return dbfile;
            }
        } catch (Exception e) {
        	
        }
        return null;
    }
    
    
    public List<GridFSDBFile> retrieveFileList(String collectionName, Map<String, String> map) {
        try {
            DB db = mongoTemplate.getMongoDbFactory().getLegacyDb();
            // 获取fs的根节点
            GridFS gridFS = new GridFS(db, collectionName);
            DBObject query =  new BasicDBObject(map);
            List<GridFSDBFile> dbfiles = gridFS.find(query);
            if (dbfiles != null) {
                return dbfiles;
            }
        } catch (Exception e) {
        	
        }
        return null;
    }
}
